var express = require("express");
var router = express.Router();
const {
  commodityTypeModel,
  commodityClassifyModel,
  commodityTypePropertyModel,
  commodityTypeParametersModel,
  commodityListModel,
  skuModel,
} = require("../model/commodity");
const { BrandModel } = require("../model/model");
// 获取商品类型
router.get("/commodityType", async (req, res) => {
  const { pageNum, pageSize } = req.query;
  const list = await commodityTypeModel
    .find()
    .skip((pageNum - 1) * pageSize)
    .limit(pageSize);

  const list_parameters = await commodityTypeParametersModel.find(); //商品类型属性
  const list_property = await commodityTypePropertyModel.find(); //商品类型参数

  const data_list = [];

  list.forEach((ele) => {
    ele.attributeCount = 0;
    ele.paramCount = 0;

    list_property.forEach((item) => {
      if (ele.id == item.productAttributeCategoryId) {
        ele.attributeCount += 1;
      }
    });

    list_parameters.forEach((item) => {
      if (ele.id == item.productAttributeCategoryId) {
        ele.paramCount += 1;
      }
    });

    data_list.push(ele);
  });

  const total = await commodityTypeModel.find().count();
  const pageTotal = Math.ceil(total / pageSize);

  res.send({
    code: 200,
    data: {
      data_list,
      total,
      pageNum,
      pageSize,
    },
    msg: "获取成功",
  });
});

// 获取商品分类基础展示页面
router.get("/commodityClassify", async (req, res) => {
  const { pageNum, pageSize } = req.query;
  const list = await commodityClassifyModel
    .find({ parentId: 0 })
    .skip((pageNum - 1) * pageSize)
    .limit(pageSize);
  const total = await commodityClassifyModel.find({ parentId: 0 }).count();
  res.send({
    code: 200,
    data: {
      list,
      total,
      pageNum,
      pageSize,
    },
    msg: "获取成功",
  });
});

// 获取商品分类用于检索条件
router.get("/commodityClassifySearch", async (req, res) => {
  const data = await commodityClassifyModel.find().lean();

  // console.log('当前商品的分类数据data===', data);

  let list = [];

  data.forEach((ele) => {
    if (ele.parentId == 0) {
      list.push(ele);
    } else {
      list.forEach((item) => {
        if (item.id == ele.parentId) {
          if (!item.children) {
            item.children = [];
          }
          item.children.push(ele);
        }
      });
    }
  });

  // Transform function
  function transformData(data) {
    return data.map((category) => ({
      value: category.id,
      label: category.name,
      children: category.children.map((child) => ({
        value: child.id,
        label: child.name,
      })),
    }));
  }

  const newList = transformData(list);

  res.send({
    code: 200,
    newList,
  });
});

// 获取商品列表
router.get("/commodityList", async (req, res) => {
  const {
    pageNum,
    pageSize,
    goBrandId,
    goProductCategoryId,
    goProductName,
    goProductSn,
  } = req.query;

  let query = { $and: [] };
  if (goBrandId) {
    query.$and.push({ brandId: goBrandId });
  }
  if (goProductCategoryId) {
    query.$and.push({ productCategoryId: goProductCategoryId });
  }
  if (goProductName) {
    query.$and.push({ name: { $regex: goProductName } });
  }
  if (goProductSn) {
    query.$and.push({ productSn: { $regex: goProductSn } });
  }
  if (query.$and.length == 0) {
    query = {};
  }
  const list = await commodityListModel
    .find(query)
    .skip((pageNum - 1) * pageSize)
    .limit(pageSize);
  const total = await commodityListModel.find(query).count();
  res.send({
    code: 200,
    data: {
      list,
      total,
      pageNum,
      pageSize,
    },
    msg: "获取成功",
  });
});

// 获取对应的sku 数据
router.get("/sku", async (req, res) => {
  const { id, productAttributeCategoryId } = req.query;
  const list_sku = await skuModel.find({ productId: id });
  const list_property = await commodityTypePropertyModel.find({
    productAttributeCategoryId,
  });
  res.send({
    data: {
      list_sku,
      list_property,
    },
  });
});

// 获取商品品牌用于检索
router.get("/brandSearch", async (req, res) => {
  const list = await BrandModel.find();

  const newList = list.map((category) => ({
    value: category.id,
    label: category.name,
  }));

  res.send({
    code: 200,
    data: {
      newList,
    },
  });
});


// 获取商品分类的详情
router.get("/categoryDetail", async (req, res) => {
  const { id } = req.query;
  const list = await commodityClassifyModel.find({ parentId: id });
  res.send({
    list
  })
})

// 获取商品属性
router.get('/productAttribute', async (req, res) => {
  const { id } = req.query;
  const list = await commodityTypePropertyModel.find({
    productAttributeCategoryId: id,
  })
  res.send({
    list
  })
})

// 获取商品属性
router.get('/parameter', async (req, res) => {
  const { id } = req.query;
  const list = await commodityTypeParametersModel.find({
    productAttributeCategoryId: id,
  })
  res.send({
    list
  })
})


module.exports = router;
